import { SCHEMA_ERROR } from '@design/core';
import { isForm } from '@design/constant/component-type';
import { isNumber } from '@design/core/utils/is-object';

const createDataSource = () => {
  return {
    type: 'static',
    value: [ '选项1', '选项2' ],
  };
};

const normalize = (error, node) => {
  let { data } = node;
  switch (error.code) {
    case SCHEMA_ERROR.DATA_INVALID:
      if (error.key === 'datasource') {
        data = data.set('datasource', createDataSource());
        return node.set('data', data);
      }
      if (error.key === 'direction') {
        data = data.set('direction', 'horizon');
        return node.set('data', data);
      }
      break;
    default:
      break;
  }
  return node;
};

export default {
  block: {
    textInput: {
      isVoid: true,
      isInline: true,
    },
    text: {
      isVoid: true,
    },
    divider: {
      isVoid: true,
    },
    select: {
      isVoid: true,
      isInline: true,
      data: {
        datasource: datasource => {
          if (!datasource) {
            return false;
          }
          return datasource.type === 'static' || datasource.type === 'dynamic';
        },
      },
      normalize,
    },
    textArea: {
      isVoid: true,
    },
    datePicker: {
      isVoid: true,
      isInline: true,
      data: {
        timeFormat: timeFormat => {
          return Array.isArray(timeFormat);
        },
        dateFormat: dateFormat => {
          return Array.isArray(dateFormat);
        },
      },
      normalize: (error, node) => {
        if (error.code !== SCHEMA_ERROR.DATA_INVALID) {
          return;
        }
        let { data } = node;
        switch (error.key) {
          case 'timeFormat':
            data = data.set('timeFormat', [ 'HH', 'mm', 'ss' ]);
            return node.set('data', data);
          case 'dateFormat':
            data = data.set('dateFormat', [ 'YYYY', 'MM', 'DD' ]);
            return node.set('data', data);
          default:
            break;
        }
      },
    },
    upload: {
      isVoid: true,
    },
    radio: {
      isVoid: true,
      data: {
        datasource: datasource => {
          if (!datasource) {
            return false;
          }
          return datasource.type === 'static' || datasource.type === 'dynamic';
        },
        direction: direction => {
          return direction === 'vertical' || direction === 'horizon';
        },
      },
      normalize,
    },
    checkbox: {
      isVoid: true,
      data: {
        datasource: datasource => {
          if (!datasource) {
            return false;
          }
          return datasource.type === 'static' || datasource.type === 'dynamic';
        },
        direction: direction => {
          return direction === 'vertical' || direction === 'horizon';
        },
      },
      normalize,
    },
  },
  data: {
    labelAlign: (align, data, node) => {
      if (!isForm(node.type)) {
        return true;
      }

      if (align === 'top' || align === 'bottom' || align === 'middle') {
        return true;
      }
      return false;
    },
    labelMargin: (margin, data, node) => {
      if (!isForm(node.type)) {
        return true;
      }

      if (isNumber(margin)) {
        return true;
      }
      return false;
    },
  },

  normalize: (error, node) => {
    if (error.code !== SCHEMA_ERROR.DATA_INVALID) {
      return;
    }
    let { data } = node;
    switch (error.key) {
      case 'labelAlign':
        data = data.set('labelAlign', 'middle');
        return node.set('data', data);
      case 'labelMargin':
        data = data.set('labelMargin', 10);
        return node.set('data', data);
      default:
        break;
    }
  },
};
